package com.changgou.search.controller;

import com.changgou.search.service.SearchService;

import java.util.Map;

@RestController
@RequestMapping("/sku_search")
public class SearchController {

  @Autowired
    private EsManagerService esManagerService;
    
  @Autowired
    private SearchService searchService;


      /**
      * 全文检索    接收前端传的查询条件
      * @return
      */
  @GetMapping
  public Map search(@RequestParam Map<String, String> paramMap) throws Exception{
        //特殊符号处理的方法
    handlerSearchMap(searchMap);
    Map resultMap = searchService.search(paramMap);
    return resultMap;
  }

  //get请求,参数都是拼接在路径上的.   对搜索入参带有特殊符号进行处理
  public void handlerSearchMap(Map<String, String> searchMap) {
    if (searchMap != null) {
      Set<Map.Entry<String, String >> entries = searchMap.entrySet();
      for (Map.Entry < String, String > entry :entries){ //遍历map
        if (entry.getKey().startsWith("spec_")) { //获取key,并且以"spec_"开头的,我们就要对里面的内容进行转换
          //对于特殊符号%2B,用+替换
          searchMap.put(entry.getKey(), entry.getValue().replace("+", "%2B"));
        }
      }
    }
  }

}
